<template>
  <div class="create-post-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="right" label-width="100px" class="surveys-form">
      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="标题" prop="headline">
            <el-input v-model="postForm.headline" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="Code" prop="code">
            <el-input v-model="postForm.code" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="9" :offset="3">
          <el-form-item label="类型" prop="type">
            <el-select
              v-model="postForm.type"
              placeholder="请选择"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
            <div class="form-tips text-warning">类型为调研时可设置题目跳转逻辑，为互动时有答题倒计时。</div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="上线" prop="online">
            <el-switch v-model="postForm.online" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="可重复答题" prop="repeat">
            <el-switch v-model="postForm.repeat" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="mt30">
        <el-col :span="18" :offset="3">
          <el-form-item>
            <el-button type="primary" @click="submitForm">提 交</el-button>
            <el-button @click="resetForm">重置</el-button>
            <el-divider v-if="isEdit" direction="vertical" />
            <el-button
              v-if="isEdit"
              icon="el-icon-document-checked"
              type="success"
              plain
              @click="toSurveysQuestions"
            >设置题目</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  getSurveysList,
  addSurveys,
  editSurveys
} from '@/api/surveys'

const defaultForm = {
  id: undefined,
  headline: '',
  code: '',
  type: '',
  online: 1,
  repeat: 1
}

export default {
  name: 'SurveysForm',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      typeOptions: [
        {
          name: '调研',
          id: 0
        },
        {
          name: '互动',
          id: 1
        }
      ],
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        headline: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    }
  },
  methods: {
    fetchData(id) {
      getSurveysList({ id }).then(response => {
        const data = response.data[0]
        // console.log(data)
        this.postForm.id = data.id
        this.postForm.headline = data.headline
        this.postForm.code = data.code
        this.postForm.type = data.type
        this.postForm.online = data.online
        this.postForm.repeat = data.repeat
      }).catch(err => {
        console.log(err)
      })
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.isEdit) {
            editSurveys(this.postForm).then(response => {
              // console.log(response)
              this.$notify({
                title: '成功',
                message: '编辑成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.$router.go(-1)
            }).catch(err => {
              console.log(err)
            })
          } else {
            addSurveys(this.postForm).then(response => {
              // console.log(response)
              this.$notify({
                title: '成功',
                message: '添加成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.$router.go(-1)
            }).catch(err => {
              console.log(err)
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.postForm.resetFields()
    },
    toSurveysQuestions() {
      const id = this.$route.params && this.$route.params.id
      this.$router.push(`/surveys/list/${id}/questions`)
    }
  }
}
</script>

<style lang="scss" scoped>
.create-post-container {
  position: relative;
  .surveys-form {
    padding: 40px;
  }
}
</style>
